package org.cms.config;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

@Configuration
public class JacksonConfig {

    private final String pattern = "yyyy-MM-dd HH:mm:ss";
    private final String TIME_ZONE = "Asia/Shanghai";


    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
        return builder -> {
            // 时区配置
            builder.timeZone(TimeZone.getTimeZone(TIME_ZONE))
                    .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

            // 序列化配置
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            builder.serializers(new LocalDateTimeSerializer(formatter));
            builder.deserializers(new LocalDateTimeDeserializer(formatter));

            // 格式化配置
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            dateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
            builder.dateFormat(dateFormat);
        };
    }
}